/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package group2assignone.view.manager;

import group2assignone.func.UCCtoALAdapter;
import group2assignone.model.Airport;
import group2assignone.ucm.manager.UCCancelAction;
import group2assignone.ucm.manager.*;
import group2assignone.view.OurView;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

/**
 *Allows selection of an airport to edit
 * @author Group 2
 */
public class EditAirportView extends OurView
{
    private JLabel title = new JLabel("Edit Airport");

    private JButton cancel = new JButton("Cancel");
    private JButton edit = new JButton("Edit");
    private AirportTableView airportView;
    private ArrayList<Airport> airportList;
    private JPanel editPanel = new JPanel();
    private JButton back = new JButton("Back");
    private JButton done = new JButton("Done");

    JLabel airportLoc = new JLabel("Airport Location: ");
    JLabel airportTax = new JLabel("Airport tax: ");
    JTextField aLoc = new JTextField();
    JTextField aTax = new JTextField();
    String oldAirportLoc;

   /**
     * generates view with table of airports
     * @param arrayList<Airport>
     *
     */
    public EditAirportView(ArrayList<Airport> airportList)
    {
        this.airportList = airportList;
        airportView = new AirportTableView(airportList);
    }



      /**
     * adds components to the GUI
     */
    @Override
    public void addComponents()
    {
        southPanel.add(cancel);
        southPanel.add(edit);

        centerPanel.add(airportView);

        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        northPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        southPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        cancel.addActionListener(new UCCtoALAdapter(new UCCancelAction(this)));
        edit.addActionListener(new UCCtoALAdapter(new UCEditAirport(this)));
        done.addActionListener(new UCCtoALAdapter(new UCEditAirportDone(this)));
        back.addActionListener(new UCCtoALAdapter(new UCEditAirportBack(this)));


        editPanel.setLayout(new GridLayout(2,2));
        editPanel.add(airportLoc);
        editPanel.add(aLoc);
        editPanel.add(airportTax);
        editPanel.add(aTax);
        pack();
    }

    public void showEditPanel()
    {
        String[] data = getRow();
        aLoc.setText(data[0]);
        aTax.setText(data[1]);

        centerPanel.remove(airportView);
        centerPanel.add(editPanel);
        southPanel.remove(edit);
        southPanel.add(back);
        southPanel.add(done);
        pack();
        oldAirportLoc = data[0];
    }

    public void showAirportList()
    {
        centerPanel.remove(editPanel);
        centerPanel.add(airportView);
        southPanel.remove(done);
        southPanel.remove(back);
        southPanel.add(edit);

        pack();
    }

    public void loadDetails(Airport airport)
    {
        airport.setAirportLoc(aLoc.getText());
        airport.setAirportTax(Double.valueOf(aTax.getText()));
        return;
    }

    public String getOldAirportLoc()
    {
        return oldAirportLoc;
    }
/**
     * this function returns the selected row number
     * this is to enable deleting and editing of the correct table row
     * @return
     */
    public String[] getRow() {
        return airportView.getRow();

    }

    /**
     * This function returns the string entered into the textfield Username
     * @return
     */
    public String getAirportLoc()
    {
        return airportView.getAirportLoc();
    }

    /**
     * This function is to set the selected row in program
     * @param row
     */
    public void setSelectedRow(int row)
    {
        airportView.setSelectedRow(row);
    }

    /**
     * this function is to return the private variable which holds the selected row
     * @return
     */
    public int getSelectedRow() {
        return airportView.getSelectedRow();
    }

}
